package com.wofish.web.controller.goods;

import com.wofish.common.annotation.Log;
import com.wofish.common.core.controller.BaseController;
import com.wofish.common.core.domain.AjaxResult;
import com.wofish.common.core.page.TableDataInfo;
import com.wofish.common.enums.BusinessType;
import com.wofish.common.json.JsonUtil;
import com.wofish.common.utils.poi.ExcelUtil;
import com.wofish.goods.domain.Goods;
import com.wofish.goods.domain.GoodsCat;
import com.wofish.goods.domain.Image;
import com.wofish.goods.dto.PreviewImage;
import com.wofish.goods.service.IGoodsCatService;
import com.wofish.goods.service.IGoodsService;
import com.wofish.goods.service.IGoodsTypeService;
import com.wofish.goods.service.IImageService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品 信息操作处理
 * 
 * @author wofish
 * @date 2019-05-31
 */
@Controller
@RequestMapping("/goods/goods")
public class GoodsController extends BaseController {

    private String prefix = "goods/goods";
	
	@Autowired
	private IGoodsService goodsService;

	@Autowired
	private IGoodsCatService goodsCatService;

	@Autowired
	private IImageService imageService;
	
	@RequiresPermissions("goods:goods:view")
	@GetMapping()
	public String goods() {
	    return prefix + "/goods";
	}
	
	/**
	 * 查询商品列表
	 */
	@RequiresPermissions("goods:goods:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Goods goods) {
		goods.setOrderFields(" create_time desc ");
		startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);

		return getDataTable(list);
	}
	



	/**
	 * 导出商品列表
	 */
	@RequiresPermissions("goods:goods:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Goods goods) {
    	List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "goods");
    }
	
	/**
	 * 新增商品
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增商品
	 */
	@GetMapping("/sku")
	public String sku() {
		return prefix + "/sku";
	}
	
	/**
	 * 新增保存商品
	 */
	@RequiresPermissions("goods:goods:add")
	@Log(title = "商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Goods goods) {
		return toAjax(goodsService.insertGoods(goods));
	}

	/**
	 * 修改商品
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
		Goods goods = goodsService.selectGoodsById(goodsId);
		mmap.put("goods", goods);
		if(goods != null){
			GoodsCat goodsCat = goodsCatService.selectGoodsCatById(goods.getCatId());
			mmap.put("goodsCat", goodsCat);
		}

		//查询是否有关联图片和默认图片
		List<Image> imageList = imageService.queryImagesByGoodsId(goodsId);

		String imageIds = "";
		String imageUrls = "";
		List<PreviewImage> initialPreviewConfig = new ArrayList<>();

		if (CollectionUtils.isNotEmpty(imageList)) {
			imageIds = imageList.stream().map(bean -> bean.getImageId()).collect(Collectors.joining(","));
			goods.setImageIds(imageIds);

			imageUrls = imageList.stream().map(bean -> bean.getUrl()).collect(Collectors.joining(","));


			initialPreviewConfig = imageList.stream().map(bean -> {
				PreviewImage previewImage = new PreviewImage();
//				previewImage.setCaption(bean.getImageName());
				previewImage.setKey(bean.getImageId());
				return previewImage;
			}).collect(Collectors.toList());
		}
		mmap.put("imageIds", imageIds);
		mmap.put("imageUrls", imageUrls);
		mmap.put("initialPreviewConfig", JsonUtil.objToStr(initialPreviewConfig));
		return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品
	 */
	@RequiresPermissions("goods:goods:edit")
	@Log(title = "商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Goods goods) {
		Date date = new Date();
		goods.setUpdateTime(date);
		return toAjax(goodsService.updateGoods(goods));
	}
	
	/**
	 * 删除商品
	 */
	@RequiresPermissions("goods:goods:remove")
	@Log(title = "商品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(goodsService.deleteGoodsByIds(ids));
	}
	
}
